<?php

/**
 * @author     Thibaut Cromphaut and Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Safti-Kado
 */
class UserController extends Zend_Controller_Action
{

    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }

    /**
     * Deze methode wordt uitgevoerd nog voor een Action.
     */
    public function preDispatch()
    {
        // Stuur de ingelogde gebruiker naar de homepagina, tenzij er uitgelogd wordt.
        if ( Zend_Auth::getInstance()->hasIdentity() ) {
            switch ( $this->getRequest()->getActionName() ) {
                case 'logout':
                    break;
                default:
                    $this->_helper->redirector('index', 'index');
                    break;
            }
        } else {
            // Uitloggen kan niet als je niet uitgelogd bent.
            switch ( $this->getRequest()->getActionName() ) {
                case 'logout':
                    $this->_helper->redirector('index');
                    break;
                default:
                    break;
            }
        }
    }

    public function indexAction()
    {
        $view = $this->_getView('Aanmelden');
        $view->form = $this->_getLoginForm();
    }
    
    public function authAction()
    {
        $request = $this->getRequest();
        
        $view = $this->_getView('Fout - Aanmelden');

        $form = $this->_getLoginForm();

        // Formulier valideren
        if ( !$form->isValid( $request->getPost() ) ) {
            $this->view->form = $form;
            return $this->render('index'); // Render index view script
        }
        
        // Gebruikersauthentificatie
        $values = $form->getValues();
        $adapter = $this->_getAuthAdapter($values);
        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($adapter);
        if ( !$result->isValid() ) {
            $form->setDescription('Ongeldige gebruikersnaam en/of wachtwoord');
            $this->view->form = $form;
            return $this->render('index'); // Render index view script
        } else {
            $storage = $auth->getStorage();
            $storage->write( $adapter->getResultRowObject(array(
                'user_role',
                'user_givenname',
                'user_familyname',
            )));
        }

        return $this->_helper->redirector('index');
    }
    
    public function logoutAction()
    {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();

        return $this->_helper->redirector('index');
    }

    public function registrationAction()
    {
        $view = $this->_getView('Registratie');
        $view->form = $this->_getRegistrationForm();
    }
    
    public function addAction()
    {
        $request = $this->getRequest();
        
        $view = $this->_getView('Fout - Toevoegen');

        // Formulier valideren
        $form = $this->_getRegistrationForm();
        if ( !$form->isValid( $request->getPost() ) ) {
            $view->form = $form;
            return $this->render('registration'); // Render registration view script
        }

        // Gebruiker toevoegen
        $values     = $form->getValues();
        $user       = new Application_Model_User($values);
        $userMapper = new Application_Model_UserMapper();
        $userMapper->save($user);
        
        return $this->_helper->redirector('index');
    }
    

    /**
     * @return Application_Form_Login
     */
    private function _getLoginForm()
    {
        return new Application_Form_Login(array(
            'action' => $this->view->baseUrl() . '/user/auth',
            'method' => 'post',
        ));
    }

    /**
     * @return Application_Form_Registration 
     */
    private function _getRegistrationForm()
    {
        return new Application_Form_Registration(array(
            'action' => $this->view->baseUrl() . '/user/add',
            'method' => 'post',
        ));
    }
    
    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css');

        return $view;
    }
    
    /**
     * @param array $values
     * @return Zend_Auth_Adapter_DbTable
     */
    private function _getAuthAdapter(array $values)
    {
        $authAdapter = new Ahs_Auth_Adapter_DbTable();
        $authAdapter->setIdentity($values['email'])
                    ->setCredential($values['password'])
        ;

        return $authAdapter;
    }
    

}

